package com.huaxinkai.mobile.util.wechatUtil;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import com.huaxinkai.mobile.entity.AccessToken;

import net.sf.json.JSONObject;

/**
 * 微信access_token
 * @author liuzh
 * @data 2017年1月1日下午4:00:39
 */
public class WechatAccessToken {
	
	public void testAccessToken(){
		AccessToken at = WechatAccessToken.getAccessToken();
		System.out.println("票据："+at.getToken());
		System.out.println("有效时间："+at.getExpiresIn());
	}
	
	/**
	 * 获取token
	 * @return
	 */
	public static AccessToken getAccessToken(){
		AccessToken token = new AccessToken();
		String url = WechatConfig.WECHAT_ACCESS_TOKEN_URL.replace("APPID", WechatConfig.WECHAT_APPID)
				.replace("APPSECRET", WechatConfig.WECHAT_APPSECRET);
		JSONObject jsonObject = doGetStr(url);
		
		if(jsonObject != null){
			token.setToken(jsonObject.getString("access_token"));
			token.setExpiresIn(jsonObject.getInt("expires_in"));
		}
		return token;
	}
	/**
	 * update 本地 AccessToken
	 * @return
	 */
	public static AccessToken updateAccessToken(){
		AccessToken token = new AccessToken();
		String url = WechatConfig.UPDATE_ACCESS_TOKEN_URL;
		JSONObject jsonObject = doGetUpdate(url);
		
		if(jsonObject != null){
			token.setToken(jsonObject.getString("token"));
			token.setExpiresIn(jsonObject.getInt("expiresIn"));
		}
		return token;
	}
	
	/**
	 * get获取数据
	 * @param url
	 * @return
	 */
	public static JSONObject doGetStr(String url){
		
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		JSONObject jsonObject = null;
		
		try {
			
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			
			if(entity != null){
				
				String result = EntityUtils.toString(entity,"UTF-8");
				
				System.out.println("HttpEntity ToString 后的结果:" + result);
				
				jsonObject = JSONObject.fromObject(result);
				
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonObject;
	}
	
	/**
	 * get更新本地access_token
	 * @param url
	 * @return
	 */
	public static JSONObject doGetUpdate(String url){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpGet httpGet = new HttpGet(url);
		JSONObject jsonObject = null;
		try {
			HttpResponse httpResponse = httpClient.execute(httpGet);
			HttpEntity entity = httpResponse.getEntity();
			if(entity != null){
				String result = EntityUtils.toString(entity,"UTF-8");
				jsonObject = JSONObject.fromObject(result);
			}
		} catch (Exception e) {
		}
		return jsonObject;
	}
	/**
	 * post获取数据
	 * @param url
	 * @return
	 */
	public static JSONObject doPostStr(String url,String outStr){
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost(url);
		JSONObject jsonObject = null;
		httpPost.setEntity(new StringEntity(outStr,"UTF-8"));
		try {
			HttpResponse response = httpClient.execute(httpPost);
			String result = EntityUtils.toString(response.getEntity(),"UTF-8");
			jsonObject = JSONObject.fromObject(result);
		} catch (Exception e) {
		}
		return jsonObject;
	}
}
